package com.hengxinguotong.zhihuichengjian.ui.app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.bean.Authority;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.widget.view.MyDynamicBox;

/**
 * Created by Administrator on 2017/3/13.
 */

public abstract class BaseFragment extends Fragment {
    protected MyDynamicBox myDynamicBox;
    protected Authority authority;
    protected static AppCompatActivity baseActivity;
    /*该页面是否已经准备完毕（onCreateView方法是否调用完毕）*/
    private boolean isPrepared = false;
    /*该页面是否已经执行过懒加载*/
    private boolean isLazyLoaded = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseActivity = (AppCompatActivity) getActivity();
        authority = (Authority) baseActivity.getIntent().getSerializableExtra("authority");
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        lazyLoad();
    }

    private void lazyLoad(){
        if(getUserVisibleHint() && isPrepared && !isLazyLoaded){
            onLazyLoad();
            isLazyLoaded = true;
        }
    }

    public abstract void onLazyLoad();

    public void getMinImage(String type){
    }

    protected void showEmptyView(View view) {
        if(myDynamicBox==null){
            myDynamicBox = new MyDynamicBox(getActivity(), view);
        }
        myDynamicBox.showCustomView(MyDynamicBox.EMPTY);
    }

    public void showToast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    protected void dismissEmptyView() {
        if(myDynamicBox!=null){
            myDynamicBox.hideAll();
        }
    }

    protected boolean hasList() {
        if (authority == null) {
            return false;
        }
        if (authority.getList() == 1) {
            return true;
        }
        return false;
    }
}
